package top.javai.www.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import top.javai.www.service.CacheManager;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class CacheManagerImpl implements CacheManager {
    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 放入redis中
     * @param key
     * @param value
     * @param expireSecond
     * @return
     */

    @Override
    public boolean set(String key, String value, long expireSecond) {
        try {
            ValueOperations<String, String> ops = redisTemplate.opsForValue();
            ops.set(key,value);
            if (expireSecond > 0){
                //设置有效期
                redisTemplate.expire(key,expireSecond, TimeUnit.SECONDS);
            }
        }catch (Exception e){
            System.out.println("放入redis出错了!");
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 取出redis中的值
     * @param key
     * @return
     */
    @Override
    public String getFromRedis(String key) {
        String value = redisTemplate.opsForValue().get(key);
        return value;
    }

    /**
     * 删除redis中的值
     * @param key
     * @return
     */
    @Override
    public boolean delKey(String key) {
        Boolean delete = redisTemplate.delete(key);
        return delete;
    }

    /**
     * 自增变量值 步长为1
     * @param key
     * @return
     */
    @Override
    public long getAutoIncrementId(String key) {
        //第二个参数就是增加的步长,也就是增幅
        Long increment = redisTemplate.opsForValue().increment(key, 1);
        return increment;
    }

    /**
     * 设置有效期
     * @param key
     * @param expireSecond
     * @return
     */
    @Override
    public boolean setExpire(String key, long expireSecond) {
        Boolean expire = redisTemplate.expire(key, expireSecond, TimeUnit.SECONDS);
        return expire;
    }

    /**
     * 使用setNx方式来放值
     * @param key
     * @param value
     * @param expireSecond
     * @return
     */
    @Override
    public boolean setNx(String key, String value, long expireSecond) {
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(key, value);
        if (expireSecond > 0){
            redisTemplate.expire(key,expireSecond,TimeUnit.SECONDS);
        }
        return aBoolean;
    }

    /**
     * 先get再set
     * @param key
     * @param value
     * @return
     */
    @Override
    public String getSet(String key, String value) {
        String andSet = redisTemplate.opsForValue().getAndSet(key, value);
        return andSet;
    }

    /**
     * list的lpush
     * @param key
     * @param values
     * @return
     */
    @Override
    public long Lpush(String key, String[] values) {
        Long aLong = redisTemplate.opsForList().leftPushAll(key, values);
        return aLong;
    }

    /**
     * 从左边开始弹弹弹,弹走鱼尾纹
     * @param key
     * @return
     */
    @Override
    public String lPop(String key) {
        String value = redisTemplate.opsForList().leftPop(key);
        return value;
    }

    @Override
    public List<String> lRange(String key, long start, long end) {
        List<String> range = redisTemplate.opsForList().range(key, start, end);
        return range;
    }

    @Override
    public boolean hSet(String key, String field, String value) {
        redisTemplate.opsForHash().put(key,field,value);
        return true;
    }

    @Override
    public String hGet(String key, String field) {
        String value =(String) redisTemplate.opsForHash().get(key, field);
        return value;
    }

    @Override
    public boolean hDel(String key, String... field) {
        Long delete = redisTemplate.opsForHash().delete(key, field);
        return true;//我规定了不出异常就是成功
    }

    @Override
    public boolean hMset(String key, Map<String, String> values) {
        redisTemplate.opsForHash().putAll(key,values);
        return true;
    }

    @Override
    public List hMget(String key, String... field) {
        List<Object> objects = redisTemplate.opsForHash().multiGet(key, Arrays.asList(field));
        return objects;
    }

    @Override
    public Map hGetAll(String key) {
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
        return entries;
    }

    @Override
    public Long sAdd(String key, String[] values) {
        Long add = redisTemplate.opsForSet().add(key, values);
        return add;
    }

    @Override
    public long sCard(String key) {
        Long size = redisTemplate.opsForSet().size(key);
        return size;
    }

    @Override
    public String sPop(String key) {
        String pop = redisTemplate.opsForSet().pop(key);
        return pop;
    }
}
